package com.xxxx.yeb.service.impl;

import com.xxxx.yeb.mapper.AdminRoleMapper;
import com.xxxx.yeb.mapper.MenuMapper;
import com.xxxx.yeb.pojo.RespBean;
import com.xxxx.yeb.pojo.Role;
import com.xxxx.yeb.mapper.RoleMapper;
import com.xxxx.yeb.service.IRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhoubin
 * @since 2020-07-17
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {

    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private AdminRoleMapper adminRoleMapper;
    @Autowired
    private MenuMapper menuMapper;


    /*
     * 查询当前所有用户角色
     * */
    @Override
    public List<Role> queryAllRole() {
        return roleMapper.queryAllRole();
    }

    /*
    * 对用户角色进行修改
    * */
    @Override
    public RespBean updateUserRoleById(Integer adminId, Integer[] rids) {
         adminRoleMapper.deleteRoleByUserId(adminId);
        //List<Integer> integers = Arrays.asList(rids);
        adminRoleMapper.insertRoles(adminId, Arrays.asList(rids));
        return  RespBean.success("更新成功");
    }

    @Override
    public RespBean deleteRole(Integer rid) {
        if (rid!=null){
            adminRoleMapper.deleteAdminRoleByRoleId(rid);
            adminRoleMapper.deleteMenuRoleByRoleId(rid);
            adminRoleMapper.deleteRoleByRoleId(rid);
            return RespBean.success("删除成功");
        }
        return RespBean.error("删除失败");

    }

    @Override
    public RespBean insertRole(String name, String nameZh) {
        String role = roleMapper.queryRoleByName(name,nameZh);
         if (!StringUtils.isEmpty(role)){
            return RespBean.error("角色已存在,添加失败");
        }
       else if (name==null || nameZh==null){
            return RespBean.error("角色的name或者nameZh不能为空");
        }
       roleMapper.insertRole(name,nameZh);
        return RespBean.success("角色添加成功");
    }

    @Override
    public RespBean updatePermitsByRoleId(Integer rid, Integer[] mids) {
        if (rid==null || mids ==null){
            return RespBean.error("更新失败");
        }
        menuMapper.deleteMenuByRoleId(rid);
        menuMapper.insertMenu(rid,mids);
        return RespBean.success("更新成功");
    }


}

